<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>insert</title>
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<h1>发布微博页面</h1>
<div>
    <input type="text" v-model="weibo.content" placeholder="随意写点嘛...给个面子喽!">
    <!--name代表上传文件时文件的参数名,action表示文件上传时的路径,limit=1设置只能选择1张图片-->
    <el-upload name="picFile"
               :limit="1"
               action="/upload"
               list-type="picture-card"
               :on-preview="handlePictureCardPreview"
               :on-success="handleSuccess"
               :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <input type="button" value="发布微博" @click="insert()">
</div>
<script src="js/vue.js"></script>
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: "div",
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo: {
                    content: "", url: ""
                }
            };
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                // 当点击删除图片时方法会执行
                // file代表要删除的文件
                // file.response代表文件上传成功时,服务器响应的数据(文件名)
                axios.get("/remove?name=" + file.response).then(function () {
                    console.log("服务器图片已删除")
                });
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response, file, fileList) {
                console.log("文件上传完成,图片名=" + response);
                v.weibo.url = response;
            },
            insert() {
                // 得到用户输入的微博文本内容和图片名一起提交给服务器
                if (v.weibo.content.trim() == "" || v.weibo.url == "") {
                    alert("微博内容和图片不能为空!")
                    return;
                }
                axios.post("/weibo/insert", v.weibo).then(function (response) {
                    if (response.data == 1) {
                        alert("发表成功");
                        location.href = "/";
                    } else {
                        alert("登录已失效,请重新登录!");
                        location.href = "/login.html";
                    }
                });
            }
        }
    })
</script>
</body>
</html>